#include <buzzer/buzzer.h>

#include <arduino/Arduino.h>



namespace buzzer
{


buzzer::buzzer(int pin)
  : _pin(pin)
{ pinMode(pin, OUTPUT); }


void buzzer::play_note(char note, int duration)
{
  static int const tones[] = { 1136, 1014, 1915, 1700, 1519, 1432, 1275 };

  if (note >= 'a' && note <= 'g')
    play_tone(tones[note - 'a'], duration);
}


void buzzer::play_tone(int tone, int duration)
{
  for (long i = 0; i < duration * 1000L; i += tone * 2)
  {
    digitalWrite(_pin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(_pin, LOW);
    delayMicroseconds(tone);
  }
}


} // buzzer
